// 多端适配混入
export default {
  data() {
    return {
      // 平台信息
      platformInfo: {
        platform: '',
        isH5: false,
        isMpWeixin: false,
        isApp: false,
        statusBarHeight: 0,
        navBarHeight: 0,
        safeAreaBottom: 0
      }
    }
  },
  
  created() {
    this.initPlatformInfo()
  },
  
  methods: {
    // 初始化平台信息
    initPlatformInfo() {
      this.platformInfo.platform = this.$platform.getPlatform()
      this.platformInfo.isH5 = this.$platform.isH5()
      this.platformInfo.isMpWeixin = this.$platform.isMpWeixin()
      this.platformInfo.isApp = this.$platform.isApp()
      this.platformInfo.statusBarHeight = this.$platform.getStatusBarHeight()
      this.platformInfo.navBarHeight = this.$platform.getNavBarHeight()
      this.platformInfo.safeAreaBottom = this.$platform.getSafeArea().bottom
    },
    
    // 平台适配的页面跳转
    $tab: {
      navigateTo: (url) => {
        uni.navigateTo({ url })
      },
      redirectTo: (url) => {
        uni.redirectTo({ url })
      },
      switchTab: (url) => {
        uni.switchTab({ url })
      },
      navigateBack: (delta = 1) => {
        uni.navigateBack({ delta })
      }
    },
    
    // 适配不同平台的提示方式
    showToast(title, type = 'none') {
      // H5端使用更友好的提示
      if (this.platformInfo.isH5) {
        if (this.$refs.uToast) {
          this.$refs.uToast.show({
            title,
            type,
            duration: 2000
          })
        } else {
          uni.showToast({
            title,
            icon: type === 'success' ? 'success' : 'none',
            duration: 2000
          })
        }
      } else {
        // 小程序和APP使用原生提示
        uni.showToast({
          title,
          icon: type === 'success' ? 'success' : 'none',
          duration: 2000
        })
      }
    },
    
    // 适配不同平台的加载提示
    showLoading(title = '加载中...') {
      uni.showLoading({
        title,
        mask: true
      })
    },
    
    hideLoading() {
      uni.hideLoading()
    },
    
    // 适配不同平台的模态对话框
    async showModal(title, content, options = {}) {
      const defaultOptions = {
        title,
        content,
        showCancel: true,
        cancelText: '取消',
        confirmText: '确定'
      }
      
      const finalOptions = { ...defaultOptions, ...options }
      
      return new Promise((resolve) => {
        uni.showModal({
          ...finalOptions,
          success: (res) => {
            resolve(res)
          }
        })
      })
    },
    
    // 适配不同平台的图片选择
    async chooseImage(options = {}) {
      const defaultOptions = {
        count: 9,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera']
      }
      
      const finalOptions = { ...defaultOptions, ...options }
      
      return new Promise((resolve, reject) => {
        uni.chooseImage({
          ...finalOptions,
          success: (res) => {
            resolve(res)
          },
          fail: (error) => {
            reject(error)
          }
        })
      })
    },
    
    // 适配不同平台的图片预览
    previewImage(urls, current = 0) {
      uni.previewImage({
        urls,
        current: typeof current === 'number' ? current : urls.indexOf(current)
      })
    },
    
    // 适配不同平台的分享功能
    async shareContent(options = {}) {
      // H5端使用Web Share API或自定义分享
      if (this.platformInfo.isH5) {
        if (navigator.share) {
          try {
            await navigator.share({
              title: options.title,
              text: options.summary,
              url: options.path || window.location.href
            })
          } catch (error) {
            console.log('分享取消或失败:', error)
          }
        } else {
          // 降级处理：复制链接
          this.copyToClipboard(options.path || window.location.href)
          this.showToast('链接已复制到剪贴板', 'success')
        }
      } else {
        // 小程序和APP使用原生分享
        uni.share({
          provider: 'weixin',
          scene: 'WXSceneSession',
          type: 0,
          ...options,
          success: () => {
            this.showToast('分享成功', 'success')
          },
          fail: (error) => {
            console.error('分享失败:', error)
            this.showToast('分享失败', 'error')
          }
        })
      }
    },
    
    // 复制到剪贴板
    async copyToClipboard(text) {
      try {
        if (this.platformInfo.isH5 && navigator.clipboard) {
          await navigator.clipboard.writeText(text)
        } else {
          uni.setClipboardData({
            data: text,
            success: () => {
              this.showToast('复制成功', 'success')
            }
          })
        }
      } catch (error) {
        console.error('复制失败:', error)
        this.showToast('复制失败', 'error')
      }
    },
    
    // 获取设备信息
    getDeviceInfo() {
      return uni.getSystemInfoSync()
    },
    
    // 适配不同平台的存储
    setStorageData(key, data) {
      try {
        uni.setStorageSync(key, data)
      } catch (error) {
        console.error('存储失败:', error)
      }
    },
    
    getStorageData(key, defaultValue = null) {
      try {
        return uni.getStorageSync(key) || defaultValue
      } catch (error) {
        console.error('读取存储失败:', error)
        return defaultValue
      }
    },
    
    removeStorageData(key) {
      try {
        uni.removeStorageSync(key)
      } catch (error) {
        console.error('删除存储失败:', error)
      }
    }
  }
}